home *** CD-ROM | disk | FTP | other *** search
- Path: solon.com!not-for-mail
- From: a1s@ix.netcom.com (Andrew Snyder)
- Newsgroups: comp.lang.c,comp.lang.c.moderated
- Subject: Re: Leading and Trailing Blanks
- Date: 12 Jan 1996 11:55:29 -0600
- Organization: Netcom
- Sender: clc@solutions.solon.com
- Approved: clc@solutions.solon.com
- Message-ID: <4d67ah$1o1@solutions.solon.com>
- References: <4chh1b$685@solutions.solon.com>
- NNTP-Posting-Host: solutions.solon.com
- X-NETCOM-Date: Fri Jan 12 8:49:06 AM PST 1996
- X-Newsreader: Forte Free Agent 1.0.82
-
- mskc@io.com (Casey Claiborne) wrote:
-
-
- >Hello -
- > I am wondering if anyone out there has a program (or knows of one)
- >that allows one to strip leading and trailing blanks from a string.
- >ex:
-
- > char test[20];
- > strcpy(test," TESTING ");
- > printf("%s", test);
-
- >will produce an output like
- > TESTING
-
- >that has blanks at the beginning of "TESTING". I would like to
- >have the following result
-
- >TESTING
-
- >that has no leading blanks.
-
- >I would *greatly* appreciate any type of help or hints in working with
- >this.
-
- >I'll be looking out here, but e-mails are also appreciated :)
-
- >TIA
-
- >Casey
-
- I like this aproach
-
- char *strip(char *s)
- {
- char *start, *end, *p;
-
- start=NULL;
- end = NULL;
- for(p = s; *p; p++)
- {
- if(!isspace(*p))
- {
- if(!start)
- start = p;
- else
- end = p;
- }
- }
-
- /* p now pointes to trailling null */
-
- if(start)
- {
- if(end)
- {
- memmove(s,start,(end - start) + 1);
- /* +1 for the fence post */
- s[(end - start) + 1] = '\0';
- /* add that null */
- }
- else
- {
- memmove(s,start,(end - start) + 2);
- /* +2 gets the trailing null */
- }
- }
- return s;
- }
- Andrew Snyder
- a1s@ix.netcom.com
- char disclaimer[] = {'\0'}; /* I'm on cash net */
-